package com.acce.gxsc.page.outCar.outCarDetail;

import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.WorkModel;
import com.acce.gxsc.model.loaclBean.ResultBean;
import com.acce.gxsc.model.netBean.NetOutBatchCarBean;
import com.acce.gxsc.model.netBean.NetOutInCarBean;
import com.acce.gxsc.model.netBean.NetSimilarCarInfo;
import com.acce.sdk.base.mvp.BasePresenter;
import com.acce.sdk.base.volleyHttp.GsonUtils;
import com.acce.sdk.utils.LogUtils;
import com.acce.sdk.utils.ToastUtils;

import java.util.ArrayList;

import rx.Subscriber;
import rx.functions.Func1;

import static com.acce.gxsc.common.Constants.OUT_CAR_GATE_NO;

/**
 * Created by huxley on 2017/5/26.
 */
public class OutCarDetailPresenter extends BasePresenter<OutCarDetailView> {

    public void loadInCarList(String queryType, String queryId, String plateNum, String containNo, String hangNum, String contactName, String outCarStatus) {
        WorkModel.getInstance().loadOutCarList(queryType, queryId, plateNum, containNo, hangNum, contactName, outCarStatus).map(new Func1<ResultBean<NetSimilarCarInfo>, ResultBean<NetSimilarCarInfo>>() {
            @Override
            public ResultBean<NetSimilarCarInfo> call(ResultBean<NetSimilarCarInfo> datas) {
                if ("200".equals(datas.returnCode)) {
                    ArrayList<NetOutInCarBean> similarCarInfo = datas.result.similarCarInfo;
                    for (int i = 0; i < similarCarInfo.size(); i++) {
                        NetOutInCarBean netOutInCarBean = similarCarInfo.get(i);
                        String[] inBatchIds = netOutInCarBean.inBatchIds.split(",",-1);
                        String[] contactNames = new String[inBatchIds.length];
                        if (netOutInCarBean.contactName != null) {
                            contactNames = netOutInCarBean.contactName.split(",",-1);
                        }
                        String[] fee2Ids = new String[inBatchIds.length];
                        if (netOutInCarBean.fee2Id != null) {
                            fee2Ids = netOutInCarBean.fee2Id.split(",",-1);
                        }
                        String[] fee3Ids = new String[inBatchIds.length];
                        if (netOutInCarBean.fee3Id != null) {
                            fee3Ids = netOutInCarBean.fee3Id.split(",",-1);
                        }
                        String[] goodsInfos = new String[inBatchIds.length];
                        if (netOutInCarBean.goodsInfo != null) {
                            goodsInfos = netOutInCarBean.goodsInfo.split(",",-1);
                        }
                        netOutInCarBean.batchCarList = new ArrayList<>();
                        for (int j = 0; j < inBatchIds.length; j++) {
                            NetOutBatchCarBean batchCar = new NetOutBatchCarBean();
                            batchCar.contactName = contactNames[j];
                            batchCar.fee2Id = fee2Ids[j];
                            batchCar.fee3Id = fee3Ids[j];
                            batchCar.goodsInfo = goodsInfos[j];
                            batchCar.inBatchId = inBatchIds[j];
                            netOutInCarBean.batchCarList.add(batchCar);
                        }
                    }
                }
                return datas;
            }
        }).subscribe(new Subscriber<ResultBean<NetSimilarCarInfo>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    getView().showProgressDialog("加载中", false);
                }
            }

            @Override
            public void onNext(ResultBean<NetSimilarCarInfo> datas) {
                if (mViewRef != null) {
                    if (!"200".equals(datas.returnCode)) {
                        ToastUtils.show(datas.returnMsg);
                        return;
                    }
                    getView().loadInCarListSuccess(datas.result.similarCarInfo);
                }
            }

            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    e.printStackTrace();
                    getView().hideProgressDialog();
                }
            }
        });
    }

    public void toOutCar(String inCarId, String outCarRemarks) {
        if (!Tools.verificationId(OUT_CAR_GATE_NO)) {
            return;
        }
        WorkModel.getInstance().toOutCar(inCarId, outCarRemarks).subscribe(new Subscriber<ResultBean<Object>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    getView().showProgressDialog("离场中...", false);
                }
            }

            @Override
            public void onNext(ResultBean<Object> datas) {
                if (mViewRef != null) {
                    LogUtils.logout(GsonUtils.toJson(datas));
                    ToastUtils.show(datas.returnMsg);
                    if ("200".equals(datas.returnCode)) {
                        getView().toOutCarSuccess();
                    }
                }
            }

            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    e.printStackTrace();
                    ToastUtils.show("删除失败！");
                    getView().hideProgressDialog();
                }
            }
        });
    }
}
